import axios from "axios";
import useUserStore from "@/stores/user";
import router from "@/router/index";

// const BASEURL = "http://106.54.206.71:8848";
const BASEURL = "http://localhost:8848";
// const BASEURL = "http://8.129.161.191:8848";
const instance = axios.create({
  baseURL: BASEURL,
});
instance.interceptors.request.use(
  function (config) {
    const userStore = useUserStore();
    const token = userStore.userInfo.token;
    if (token) {
      config.headers.token = token;
    }
    return config;
  },
  function (error) {
    return Promise.reject(error);
  }
);

instance.interceptors.response.use(
  function (response) {
    if (response.data.code === 401) {
      // 跳转到登录页面
      router.push("/login");
    }
    return response;
  },
  function (error) {
    return Promise.reject(error);
  }
);
export default instance;
